﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaModel;

namespace OpenExpressApp.Module.WPF.Editors.TreeColumn
{
    /// <summary>
    /// 类似GridColumnsFactory
    /// </summary>
    public class TreeColumnsFactory
    {
        /// <summary>
        /// 默认的列类型和column编辑器的映射关系
        /// </summary>
        private static Dictionary<string, Type> treeColumnMap = new Dictionary<string, Type>() { 
            { EditorNames.String, typeof(StringTreeColumn) },
            { EditorNames.Memo, typeof(MemoTreeColumn) },
            { EditorNames.Boolean, typeof(BooleanTreeColumn) },
            { EditorNames.Enum, typeof(EnumTreeColumn) },
            { EditorNames.LookupDropDown, typeof(LookupListTreeColumn)},
        };

        /// <summary>
        /// 根据editorName生成对应的Column
        /// </summary>
        /// <param name="editorName"></param>
        /// <param name="info"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public static TreeColumn GetDefaultTreeColumn(string editorName, BusinessObjectPropertyInfo info, IListObjectView view)
        {
            return (TreeColumn)Activator.CreateInstance(GetTreeColumnType(editorName), new object[] { info, view });
        }

        /// <summary>
        /// 添加editorName到gridColumnType的映射
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        public static void AddEditor(string name, Type type)
        {
            treeColumnMap.Add(name, type);
        }

        private static Type GetTreeColumnType(string editorName)
        {
            Type editor = null;
            treeColumnMap.TryGetValue(editorName, out editor);
            return editor ?? GetDefaultPropertyEditorType();
        }
        private static Type GetDefaultPropertyEditorType()
        {
            return treeColumnMap[EditorNames.String];
        }
    }
}
